﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace RemoteScriptingLibrary
{
    public class AssemblyHelper
    {
        /// <summary>
        /// Bases the type.
        /// </summary>
        /// <param name="_type">The _type.</param>
        /// <param name="_matchingType">Type of the _matching.</param>
        /// <returns></returns>
        private static Type BaseType(Type _type, Type _matchingType)
        {
            while (_type.BaseType != null && _type != _matchingType)
            {
                _type = _type.BaseType;
            }
            return _type;
        }

        private static SortedDictionary<string, List<int>> aCommand = new SortedDictionary<string, List<int>>();

        public static SortedDictionary<string, List<int>> GetCommandsFromType(Type type)
        {
            foreach (MethodInfo info in type.GetMethods())
            {
                string sName = info.Name;
                int nParamCount = info.GetParameters().Length;
                if (aCommand.ContainsKey(sName))
                {
                    if (!aCommand[sName].Contains(nParamCount))
                    {
                        aCommand[sName].Add(nParamCount);
                    }
                }
                else
                {
                    aCommand.Add(sName, new List<int>() { nParamCount });
                }
            }
            return aCommand;
        }

        public static string PrintMethodInfo(params MethodInfo[] methods)
        {
            if (methods == null) return string.Empty;

            StringBuilder sbText = new StringBuilder();
            string sText = string.Empty;
            foreach (MethodInfo m in methods)
            {
                if (m == null) continue;
                if (m.IsStatic)
                    sbText.AppendLine(string.Format("Static Method: {0}", m.Name));
                else
                    sbText.AppendLine(string.Format("Method: {0}", m.Name));
                sbText.AppendLine(string.Format("\tReturns {0}", m.ReturnType.FullName));
                ParameterInfo[] pList = m.GetParameters();
                if (pList.Length == 0)
                {
                    sbText.AppendLine("\tNo Parameters");
                }
                else
                {
                    sbText.AppendLine("\tParameter List:");
                    foreach (ParameterInfo p in pList)
                    {
                        sbText.AppendLine(string.Format("\t\t{0} {1}", p.ParameterType.FullName, p.Name));
                    }
                    sbText.AppendLine();
                }
            }
            sText = sbText.ToString();
            return sText;
        }
        //public static IEnumerable<Type> FilterList()
        //{
        //    Assembly assembly = Assembly.GetExecutingAssembly();

        //    Type[] typeOfFilter = assembly.GetTypes();

        //    List<Type> aType = new List<Type>();

        //    foreach (Type type in typeOfFilter)
        //    {
        //        if (type.Name == "Experimental2")
        //        {
        //        }
        //        Type typeTemp = BaseType(type, typeof(BaseImageFilter));
        //        if (type.IsAbstract) continue;
        //        if (type.IsNotPublic) continue;
        //        //TODO get real base type
        //        if (type.FullName.ToLower().Contains("graysc"))
        //        {
        //        }
        //        if (type.FullName.ToLower().StartsWith("lowlevelgraphics.filter."))
        //        {
        //            aType.Add(type);
        //        }
        //        //TODO: only to debug yet
        //        else
        //        {
        //        }
        //    }
        //    return aType;
        //}
    }
}
